import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { Loading } from 'element-ui'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/detail/:detailId',
    name: 'detail',
    meta: {
      auth: true,
      title: '详情',
    },
    component: () => import('../views/Detail.vue'),
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      auth: false,
      title: '登陆',
    },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../layout/index.vue'),
    children: [
      {
        path: 'home',
        name: 'home',
        meta: {
          auth: true, // 需要判断token才能进入页面
          title: '首页',
        },
        component: async () => {
          console.log('loading-true')
          const res = await import('../views/Home.vue')
          console.log('loading-false')
          return res
        },
      },
      {
        path: 'table',
        name: 'table',
        meta: {
          auth: true, // 需要判断token才能进入页面
          title: 'table',
        },
        component: async () => {
          return await import('../views/Table.vue')
        },
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// 导航守卫
// router.beforeResolve((to, from, next) => {
//   if (to.meta.auth) {
//     const token = store.state.token
//     if (token) {
//       next()
//     } else {
//       next({
//         path: '/layout/personCenter',
//       })
//     }
//   } else {
//     next()
//   }
// })

export default router
